from datetime import date, timedelta
from random import randint
import sys
import subprocess
import os

# returns a date string for the date that is N days before STARTDATE
def get_date_string(n, startdate):
    d = startdate - timedelta(days=n)
    rtn = d.strftime("%a %b %d %X %Y") + " -0400"  # Add the time zone offset manually
    return rtn

# main app
def main(argv):
    if len(argv) < 1 or len(argv) > 2:
        print("Error: Bad input.")
        sys.exit(1)
    
    n = int(argv[0])
    
    if len(argv) == 1:
        startdate = date.today()
    elif len(argv) == 2:
        startdate = date(int(argv[1][0:4]), int(argv[1][5:7]), int(argv[1][8:10]))

    i = 0
    while i <= n:
        curdate = get_date_string(i, startdate)
        num_commits = randint(1, 10)
        for commit in range(num_commits):
            # Write date and random number to 'realwork.txt'
            with open("realwork.txt", "w") as f:
                f.write(f"{curdate}{randint(0, 1000000)}")

            # Stage the file
            subprocess.call(["git", "add", "realwork.txt"])

            # Commit the staged changes with specified date
            env = {
                **os.environ,
                "GIT_AUTHOR_DATE": curdate,
                "GIT_COMMITTER_DATE": curdate,
            }
            subprocess.call(["git", "commit", "-m", "update"], env=env)

        i += 1
    
    # Remove the file and commit the deletion
    subprocess.call(["git", "rm", "realwork.txt"])
    subprocess.call(["git", "commit", "-m", "delete"])
    subprocess.call(["git", "push"])

if __name__ == "__main__":
    main(sys.argv[1:])
